#!/usr/bin/python
from Tkinter import *
import sys
import re
import copy

root = Tk()

c = Canvas(root,width=480,height=480,background='white')
c.pack()

f = open(sys.argv[1],'r')
lines = f.readlines()
f.close()

nodes = {}
edges = {}
i = 0
while i < len(lines):
    if lines[i][0]=='#':
        i += 1
        break
    (id,val)=lines[i].split('    ',1)
    nodes[id] = eval(val)
    i += 1
assert i!=len(lines)
while i < len(lines):
    (id1,right)=lines[i].split(' ',1)
    (id2,val)=right.split('    ',1)
    edges[(id1,id2)] = val
    i += 1

arg1 = sys.argv[2]
arg2 = sys.argv[3]

arg1index = None
arg2index = None
m1 = re.match("(.+)\[(.+)\]",arg1)
if m1:
    arg1 = m1.group(1)
    arg1index = int(m1.group(2))
m2 = re.match("(.+)\[(.+)\]",arg2)
if m2:
    arg2 = m2.group(1)
    arg2index = int(m2.group(2))

print arg1,arg1index
print arg2,arg2index

def project(v,arg=None,index=None):
    if arg != None:
        if index != None:
            return v[arg][index]
        else:
            return v[arg]
    return (project(v,arg1,arg1index),project(v,arg2,arg2index))

bounds = [[0,0],[0,0]]
for v in nodes.itervalues():
    p = project(v)
    assert(isinstance(p[0],(int,float)))
    assert(isinstance(p[1],(int,float)))
    bounds[0][0] = min(bounds[0][0],p[0])
    bounds[0][1] = max(bounds[0][1],p[0])
    bounds[1][0] = min(bounds[1][0],p[1])
    bounds[1][1] = max(bounds[1][1],p[1])

size = (bounds[0][1]-bounds[0][0],bounds[1][1]-bounds[1][0])
pbounds = [(bounds[0][0],bounds[1][0]),
           (bounds[0][1],bounds[1][1])]
btext = [(bounds[0][0]-size[0]*0.05,bounds[1][0]-size[1]*0.05),
         (bounds[0][1]+size[0]*0.05,bounds[1][1]+size[1]*0.05)]
bounds[0][0] -= 0.1*size[0]
bounds[0][1] += 0.1*size[0]
bounds[1][0] -= 0.1*size[1]
bounds[1][1] += 0.1*size[1]

def plotvalue(p):
    return (int(480.0*(p[0]-bounds[0][0])/(bounds[0][1]-bounds[0][0])),\
            int(480.0*(p[1]-bounds[1][0])/(bounds[1][1]-bounds[1][0])))

def plot(v):
    p = project(v)
    return plotvalue(p)

for v in nodes.itervalues():
    p = plot(v)
    c.create_oval(p[0]-3,p[1]-3,p[0]+3,p[1]+3)

for id1,id2 in edges.iterkeys():
    p1 = plot(nodes[id1])
    p2 = plot(nodes[id2])
    c.create_line([p1[0],p1[1],p2[0],p2[1]])

pmin = plotvalue(btext[0])
pmax = plotvalue(btext[1])
c.create_text(pmin[0],pmin[1],text=("%g,%g"%pbounds[0]))
c.create_text(pmax[0],pmax[1],text=("%g,%g"%pbounds[1]))

root.mainloop()
